using System;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using MCromwell.StaffIntranet.Presenters;
using MCromwell.StaffIntranet.Task;
using MCromwell.StaffIntranet.DTO;
using System.Collections.Generic;

namespace MCromwell.StaffIntranet.UI
{
    public partial class View : Page, IStaffDetailsView

    {
        private StaffViewingPresenter presenter;
        public event StaffSelectedEventDelegate StaffSelected;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            InitializePresenter();
            BindEvents();
            StaffViewed();
        }

        private int StaffId
        {
            get
            {
                int returnValue = 0;
                string rawId = Request.QueryString["StaffId"];
                int.TryParse(rawId, out returnValue);
                return returnValue;
            }
        }

        private void BindEvents()
        {
            btnBack.Click += delegate(object sender, EventArgs e)
            {
                Response.Redirect("Search.aspx");
            };
        }
        
        private void StaffViewed()
        {
            StaffSelectedEventArgs e = new StaffSelectedEventArgs(StaffId);
            StaffSelected(this, e);
        }

        private void InitializePresenter()
        {
            presenter = new StaffViewingPresenter(this);
        }
        
        public void ShowDetailsOf(StaffDTO staffSelected)
        {
            StaffDetailsTable.Visible = true;

            lblStaffName.Text = string.Format("{0} {1}", staffSelected.FirstName, staffSelected.LastName);
            lbtnEmailAddress.Text = staffSelected.EmailAddress;
            lbtnEmailAddress.PostBackUrl = staffSelected.EmailAddress;
            lblTelephone.Text = staffSelected.TelephoneNumber;
            lblExtension.Text = staffSelected.Extension;
            lblLocation.Text = staffSelected.LocationDescription;
            imgPhoto.ImageUrl = string.Format("{0}.photo", staffSelected.Id);
            imgPhoto.AlternateText = string.Format("Photo of {0} {1}", staffSelected.FirstName, staffSelected.LastName);
        }
                
        public void DisplayError(string message)
        {
            lblMessage.Visible = true;
            lblMessage.Text = message;
        }
                
    }
}
